package com.ymatou.iapi.godmch.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.parameter.getProviderListBean;
import com.ymatou.iapi.godmch.parameter.getProviderListResp;
import com.ymatou.iapi.godmch.parameter.getProviderListResp.provider;
import com.ymatou.iapi.godmch.service.getProviderListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.serviceRelation;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.providerSupportCountryWapper;
import com.ymttest.database.sqlwapper.godmch.serviceProviderWapper;
import com.ymttest.database.sqlwapper.godmch.serviceRelationWapper;
/**
*获取服务商列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getProviderList{
	private static getProviderListBean bean;
	private static getProviderListCall call;
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ; 
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取服务商列表");
	}

	@Before
	public void caseUp() {
		bean=new getProviderListBean();
		call=new getProviderListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private String country_str(Integer providerId) throws InterruptedException
	{
		String str="";
		providerSupportCountryWapper countryWapper=new providerSupportCountryWapper();
		List<Map> clstList=countryWapper.selectNameByProviderId(providerId);
		for(int i=0;i<clstList.size();i++)
			str+=clstList.get(i).get("cnTitle")+"\\、";

		return str.substring(0, str.length()-1).replace("\\", "");
	}
	private void check_verify() throws InterruptedException, JSONException
	{
		HashMap hMap=new HashMap();
		hMap.put("name", bean.getName());
		hMap.put("providerId", bean.getProviderId());
		hMap.put("country", bean.getCountry());
		hMap.put("pageIndex", bean.getPageIndex());
		hMap.put("pageSize", bean.getPageSize());
		
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
		List<provider> resp=call.getProviderList().getProviderList();
		//total
		serviceProviderWapper providerWapper=new serviceProviderWapper();
		Integer total=providerWapper.selectTotal(hMap);
		Logger.verifyEquals(total,call.getTotal(), "验证total");
		
		List<Map> proivderlst =providerWapper.selectProviders(hMap);
		Logger.comment(" #####  proivderlst ########## "+proivderlst.size());
		for(int i=0;i<proivderlst.size();i++)
		{
			Map map=proivderlst.get(i);
			int pId=resp.get(i).getProviderId();
			Logger.verifyEquals(Integer.parseInt(map.get("providerId").toString()), pId, "验证第"+(i+1)+"条providerId");
			Logger.verifyEquals(df.format(map.get("mchCreateTime")), resp.get(i).getRegisterTime(), "验证第"+(i+1)+"条RegisterTime");
			Logger.verifyEquals(map.get("mchTitle").toString(), resp.get(i).getName(), "验证第"+(i+1)+"条Name");
			//查询代理国家
			Logger.verifyEquals(country_str(pId), resp.get(i).getCountries(), "验证第"+(i+1)+"条Countries");
			//查询渠道数
			serviceRelationWapper relationWapper=new serviceRelationWapper();
			Integer channelNum=relationWapper.selectChannelNumByproviderId(pId);
			Logger.verifyEquals(channelNum, resp.get(i).getChannelNum(), "验证第"+(i+1)+"条ChannelNum");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_001() {
		Logger.start(true,"正例-获取服务商列表-服务商有服务国家，并且有服务渠道-根据服务商名称name、国家country");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			
			int providerId=pBean.getMchId();
			//修改服务商名称
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(providerId);
			GodmchManageCallService.editmachCall(editmchBean);
			
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//添加服务关系
			GodmchManageCallService.InitAddServiceRelation(providerId,mchBean.getMchId(),1);//1出口退税 2其他

			bean.setName(editmchBean.getMchTitle());
			bean.setCountry(pBean.getCountry());
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();		

			check_verify();
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_001_3() {
		Logger.start(true,"正例-获取服务商列表-服务商有服务国家，并且有服务渠道-根据服务商id providerId查询");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			
			int providerId=pBean.getMchId();
			//修改服务商名称
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(providerId);
			GodmchManageCallService.editmachCall(editmchBean);
			
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//添加服务关系
			GodmchManageCallService.InitAddServiceRelation(providerId,mchBean.getMchId(),1);//1出口退税 2其他

            bean.setProviderId(providerId);
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();		

			check_verify();
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_001_1() {
		Logger.start(true,"正例-获取服务商列表-服务商有服务国家，并且有服务渠道-服务商名称name和国家country都为空");
		try {

			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();		

			check_verify();
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_002() {
		Logger.start(true,"反例-获取服务商列表-服务商名称不存在,但模糊查询存在");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			
			int providerId=pBean.getMchId();
			//修改服务商名称
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(providerId);
			GodmchManageCallService.editmachCall(editmchBean);
			
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//添加服务关系
			GodmchManageCallService.InitAddServiceRelation(providerId,mchBean.getMchId(),1);//1出口退税 2其他

			bean.setName("99");
			bean.setCountry(pBean.getCountry());
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();		

			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_002_1() {
		Logger.start(true,"反例-获取服务商列表-服务商名称模糊查询不存在");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			
			int providerId=pBean.getMchId();
			//修改服务商名称
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(providerId);
			GodmchManageCallService.editmachCall(editmchBean);
			
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//添加服务关系
			GodmchManageCallService.InitAddServiceRelation(providerId,mchBean.getMchId(),1);//1出口退税 2其他

			bean.setName("123456789012345678901234567890123456789");
			bean.setCountry(pBean.getCountry());
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();		

			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			//total
			Logger.verifyEquals(0,call.getTotal(), "验证total");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_003() {
		Logger.start(true,"反例-获取服务商列表-国家与服务商不一致");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			
			int providerId=pBean.getMchId();
			//修改服务商名称
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(providerId);
			GodmchManageCallService.editmachCall(editmchBean);
			
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//添加服务关系
			GodmchManageCallService.InitAddServiceRelation(providerId,mchBean.getMchId(),1);//1出口退税 2其他

			bean.setName(editmchBean.getMchTitle());
			bean.setCountry("NLD");
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			//total
			Logger.verifyEquals(0,call.getTotal(), "验证total");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_004() {
		Logger.start(true,"反例-获取服务商列表-国家表国家不存在");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			
			int providerId=pBean.getMchId();
			//修改服务商名称
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(providerId);
			GodmchManageCallService.editmachCall(editmchBean);
			
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//添加服务关系
			GodmchManageCallService.InitAddServiceRelation(providerId,mchBean.getMchId(),1);//1出口退税 2其他

			bean.setName(editmchBean.getMchTitle());
			//bean.setCountry("NLD");
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();				
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_005() {
		Logger.start(true,"反例-获取服务商列表-国家表存在国家,但是服务商列表中不存在");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			
			int providerId=pBean.getMchId();
			//修改服务商名称
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(providerId);
			GodmchManageCallService.editmachCall(editmchBean);
			
			//渠道
			createmchBean mchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			mchBean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(mchBean);
			//添加服务关系
			GodmchManageCallService.InitAddServiceRelation(providerId,mchBean.getMchId(),1);//1出口退税 2其他

			bean.setName(editmchBean.getMchTitle());
			bean.setCountry("NLD");
			bean.setPageIndex(1);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();				
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			//total
			Logger.verifyEquals(0,call.getTotal(), "验证total");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_006() {
		Logger.start(true,"反例-获取服务商列表-当前页等于0");
		try {
			
			bean.setPageIndex(0);
			bean.setPageSize(10);
			call.setData(bean);
			call.callService();		

			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_007() {
		Logger.start(true,"反例-获取服务商列表-每页显示数量等于0");
		try {
			bean.setPageIndex(1);
			bean.setPageSize(0);
			call.setData(bean);
			call.callService();		

			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证msg");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderList_008() {
		Logger.start(true,"反例-获取服务商列表-当前页大于总页数");
		try {
			bean.setPageIndex(99999);
			bean.setPageSize(1000);
			call.setData(bean);
			call.callService();		

			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			//total
			HashMap hMap=new HashMap();
			hMap.put("name", bean.getName());
			hMap.put("providerId", bean.getProviderId());
			hMap.put("country", bean.getCountry());
			hMap.put("pageIndex", bean.getPageIndex());
			hMap.put("pageSize", bean.getPageSize());
			serviceProviderWapper providerWapper=new serviceProviderWapper();
			Integer total=providerWapper.selectTotal(hMap);
			Logger.verifyEquals(total,call.getTotal(), "验证total");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_getProviderList_009() {
		Logger.start(true,"反例-获取服务商列表-每页显示数量大于总条数-case返回数据太多，平台上不运行");
		try {
			HashMap hMap=new HashMap();
			hMap.put("pageIndex", 99999);
			hMap.put("pageSize", 1000);
			serviceProviderWapper providerWapper=new serviceProviderWapper();
			Integer total=providerWapper.selectTotal(hMap);
			
			bean.setPageIndex(1);
			bean.setPageSize(total+1);
			call.setData(bean);
			call.callService();		

			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}